package com.musifind.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.musifind.common.GeneralHelper;
import com.musifind.utils.SessionManager;

public class HomepageActivity extends Activity {

	private static long back_pressed;
	private TextView txtUserName;
	private ImageView imgUserPicture;
	private Button btnListen, btnWhoWhat, btnFavs, btnMatches;
	private boolean isFacebookUser;
	private TextView txtToast;
	private View view;
	private String strUserPicture;
	SessionManager session;
	Animation animBlink;
	private Button btnInfo;
	private ProgressDialog progressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_homepage);

		// activity starts as push old activity to left
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

		loadUser();

		animBlink = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.blink_once);
		ActionBar mActionBar = getActionBar();
		mActionBar.setDisplayShowHomeEnabled(false);
		mActionBar.setDisplayShowTitleEnabled(false);
		LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
		View mCustomView = mInflater.inflate(R.layout.custom_actionbar_homepage, null);
		btnInfo = (Button) mCustomView.findViewById(R.id.btnInfo);
		btnInfo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				btnInfo.startAnimation(animBlink);
				showInfoDialog();
			}
		});
		mActionBar.setCustomView(mCustomView);
		mActionBar.setDisplayShowCustomEnabled(true);
		final int actionBarColor = getResources().getColor(R.color.ActionBarDark);
		mActionBar.setBackgroundDrawable(new ColorDrawable(actionBarColor));

		view = new View(HomepageActivity.this);

		btnListen = (Button) findViewById(R.id.btnListen);
		btnWhoWhat = (Button) findViewById(R.id.btnWhoIsListeningWhat);
		btnFavs = (Button) findViewById(R.id.btnFavourites);
		btnMatches = (Button) findViewById(R.id.btnMatches);
		btnListen.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(HomepageActivity.this, SearchActivity.class);
				startActivity(i);
			}
		});

		btnWhoWhat.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(HomepageActivity.this, WhoIsListeningWhatActivity.class);
				startActivity(i);
			}
		});

		btnFavs.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(HomepageActivity.this, FavouritesActivity.class);
				startActivity(i);
			}
		});

		btnMatches.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(HomepageActivity.this, MatchedUsersActivity.class);
				startActivity(i);
			}
		});

	}

	// double back click to exit app
	public void onBackPressed() {
		if (back_pressed + 2000 > System.currentTimeMillis()) {
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
		} else {
			GeneralHelper.showPurpleToast(getApplicationContext(), "Çıkmak için tekrar basın");
		}

		back_pressed = System.currentTimeMillis();
	}

	private void loadUser() {
		new Thread(new Runnable() {
			public void run() {
				try {

					session = new SessionManager(HomepageActivity.this);

					txtUserName = (TextView) findViewById(R.id.txtUserNameFB);
					imgUserPicture = (ImageView) findViewById(R.id.imgUserPictureFB);

					isFacebookUser = session.isFaceUser();
					strUserPicture = session.getUserPicture();

					txtUserName.setText("Hoşgeldin,\n" + session.getUsername());
					if (isFacebookUser) {
						imgUserPicture.setImageBitmap(GeneralHelper.roundCornerImage(
								GeneralHelper.stringToBitmap(strUserPicture), 95));
					} else {
						Bitmap defaultIcon = GeneralHelper.drawableToBitmap(getResources().getDrawable(
								R.drawable.pp_facebook));
						imgUserPicture.setImageBitmap(GeneralHelper.roundCornerImage(defaultIcon, 280));
					}

					txtUserName.setTextSize(17);
				} catch (Exception e) {
					Log.e("loadHomePage();", "metod çalıştırılamadı");
				}
			}
		}).start();
	}

	private void showInfoDialog() {
		final Dialog dialog = new Dialog(HomepageActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		dialog.setContentView(R.layout.dialog_info_homepage);
		dialog.show();
		TextView txtDialogListen = (TextView) dialog.findViewById(R.id.txtDialogListen);
		txtDialogListen.setText("Pek çok yeni müzik keşfedip dinleyebilir,");
		TextView txtDialogWhoWhat = (TextView) dialog.findViewById(R.id.txtDialogWhoWhat);
		txtDialogWhoWhat
				.setText("Dünyanın dört bir yanında yaşayan insanların o an ne dinlediklerini görüp onlarla eşleşebilir,");
		TextView txtDialogFavourites = (TextView) dialog.findViewById(R.id.txtDialogFavourites);
		txtDialogFavourites.setText("Beğendiğin müzikleri favorilerine saklayabilir,");
		TextView txtDialogMatches = (TextView) dialog.findViewById(R.id.txtDialogMatches);
		txtDialogMatches.setText("Daha önce eşleştiğin kişileri görüp düzenleyebilirsin!");
		Button btnOK = (Button) dialog.findViewById(R.id.btnDialogOK);
		btnOK.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	// public class LoadHomepageAsyncTask extends AsyncTask<String, Void, String> {
	// @Override
	// protected void onPreExecute() {
	// progressDialog = new ProgressDialog(HomepageActivity.this);
	// progressDialog.setTitle("Bağlantı sağlanıyor");
	// progressDialog.setMessage("Lütfen bekleyiniz...");
	// progressDialog.setCancelable(false);
	// progressDialog.show();
	// }
	//
	// @Override
	// protected String doInBackground(String... params) {
	// loadHomepage();
	// return null;
	// }
	//
	// @Override
	// protected void onPostExecute(String s) {
	// progressDialog.dismiss();
	// }
	// }
}
